package com.keith.foothandframe.hutool;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenli
 * @date 2024/1/24 9:09
 **/
public class CalculateAmountTest {
    public static void main(String[] args) {
        List<BuyerContractProductDetail> buyerContractProductDetailList = new ArrayList<>();
        BuyerContractProductDetail buyerContractProductDetail = new BuyerContractProductDetail();
        String text = "{" +
                "\"num\": \"100\"," +
                "\"preferentialAmount\": 100," +
                "\"price\": 4," +
                "\"taxFee\": 2," +
                "\"discount\": 0.8," +
                "}";
        buyerContractProductDetail = JSONUtil.toBean(text, BuyerContractProductDetail.class);
        buyerContractProductDetailList.add(buyerContractProductDetail);
        BigDecimal contractPayAmount = BigDecimal.ZERO;
        BigDecimal contractTaxAmount = BigDecimal.ZERO;
        BigDecimal contractDiscountAmount = BigDecimal.ZERO;
        BigDecimal contractTotalAmount = BigDecimal.ZERO;
        for (BuyerContractProductDetail detail : buyerContractProductDetailList) {
            // 含税单价
            BigDecimal priceTax = detail.getPrice().add(detail.getPrice()
                    .multiply(detail.getTaxFee()).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP))
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setPriceTax(priceTax);

            // 折扣单价（未税）
            BigDecimal discountPriceNoTax = detail.getPrice().multiply(detail.getDiscount())
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setDiscountPriceNoTax(discountPriceNoTax);

            // 折扣单价（含税）
            BigDecimal discountPriceTax = priceTax.multiply(detail.getDiscount())
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setDiscountPriceTax(discountPriceTax);

            // 税额
            BigDecimal taxAmount = discountPriceNoTax
                    .multiply(new BigDecimal(detail.getNum()))
                    .multiply(detail.getTaxFee()).divide(new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_UP)
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setTaxAmount(taxAmount);
            // 总税额
            contractTaxAmount = contractTaxAmount.add(taxAmount);

            // 总金额（未税）
            BigDecimal amountNoTax = discountPriceNoTax.multiply(new BigDecimal(detail.getNum()))
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setAmountNoTax(amountNoTax);

            // 总金额（含税）
            BigDecimal amountTax = amountNoTax.add(taxAmount)
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setAmountTax(amountTax);
            // 支付总金额
            contractPayAmount = contractPayAmount.add(amountTax);

            // 优惠后单价（未税）
            BigDecimal pricePreferentialNoTax = (amountNoTax.subtract(detail.getPreferentialAmount()))
                    .divide(new BigDecimal(detail.getNum()), 2, BigDecimal.ROUND_HALF_UP)
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setPricePreferentialNoTax(pricePreferentialNoTax);

            // 优惠后单价（含税）
            BigDecimal pricePreferentialTax = (amountTax.subtract(detail.getPreferentialAmount()))
                    .divide(new BigDecimal(detail.getNum()), 2, BigDecimal.ROUND_HALF_UP)
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setPricePreferentialTax(pricePreferentialTax);

            // 优惠后金额
            BigDecimal totalAmount = amountTax.subtract(detail.getPreferentialAmount())
                    .setScale(2, BigDecimal.ROUND_HALF_UP);
            detail.setTotalAmount(totalAmount);
            // 优惠后总金额
            contractTotalAmount = contractTotalAmount.add(totalAmount);

            // 优惠金额总计
            contractDiscountAmount = contractDiscountAmount.add(detail.getPreferentialAmount());

        }
    }

    @Data
    static class BuyerContractProductDetail {
        /**
         * 采购合同id
         */
        private Integer contractId;

        /**
         * 采购申请id
         */
        private Integer applyId;

        /**
         * 产品名称
         */
        private String productName;

        /**
         * 产品编号
         */
        private String productCode;

        /**
         * 产品注册证编号/备案凭证号
         */
        private String productRegistCode;

        /**
         * 规格型号
         */
        private String specModel;

        /**
         * 票据类型，0：不开票，1：增值税普通发票，2：增值税专用发票
         */
        private Integer billType;

        /**
         * 数量
         */
        private Integer num;

        /**
         * 单价
         */
        private BigDecimal price;

        /**
         * 税率
         */
        private BigDecimal taxFee;

        /**
         * 含税单价
         */
        private BigDecimal priceTax;

        /**
         * 折扣
         */
        private BigDecimal discount;

        /**
         * 折扣单价（未税）
         */
        private BigDecimal discountPriceNoTax;

        /**
         * 折扣单价（含税）
         */
        private BigDecimal discountPriceTax;

        /**
         * 税额
         */
        private BigDecimal taxAmount;

        /**
         * 总金额（未税）
         */
        private BigDecimal amountNoTax;

        /**
         * 总金额（含税）
         */
        private BigDecimal amountTax;

        /**
         * 优惠金额
         */
        private BigDecimal preferentialAmount;

        /**
         * 优惠后单价（未税）
         */
        private BigDecimal pricePreferentialNoTax;

        /**
         * 优惠后单价（含税）
         */
        private BigDecimal pricePreferentialTax;

        /**
         * 优惠后总金额
         */
        private BigDecimal totalAmount;

        /**
         * 备注
         */
        private String remark;
    }
}
